package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.Dto.ChannleDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.mapper.WmChannelMapper;
import com.heima.media.service.IWmChannelService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class IWmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements IWmChannelService   {
    @Override
    public ResponseResult listByName(ChannleDto dto) {
        //根据频道名称分页查询
        //构建分页条件
        Page<WmChannel> page = new Page<>(dto.getPage(),dto.getSize());
        //构建查询条件
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        //判断频道名称是否为空   不为空则添加条件
        if (!StringUtils.isEmpty(dto.getName())){
            //添加查询条件
            query.like(WmChannel::getName,dto.getName());
        }
        //执行分页查询
        Page<WmChannel> iPage = this.page(page, query);
        long total = iPage.getTotal();
        List<WmChannel> records = iPage.getRecords();

        //构建通用分页响应
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), total, records);
        return result;
    }

    @Override
    public ResponseResult saveWmChannel(WmChannel channel) {
        //关键数据校验
        if (channel == null || StringUtils.isEmpty(channel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //保存新增频道
        //判断是否已经存在该频道   根据频道名称查询
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        query.eq(WmChannel::getName,channel.getName());
        int count = this.count(query);
        if (count > 0){
        //如果已经存在  返回提示信息
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //不存在  保存在数据库中
        channel.setCreatedTime(new Date());
        this.save(channel);
        return ResponseResult.okResult();
    }

    /*
        频道修改
     */
    @Override
    public ResponseResult updateWmChannel(WmChannel wmChannel) {
        //关键数据校验
        if (wmChannel == null || wmChannel.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //频道状态修改 status true 表示启用  false 表示禁用
//        if (wmChannel.getStatus().equals(true)){
//            wmChannel.setStatus(true);
//            return ResponseResult.okResult();
//        }
//        if (wmChannel.getStatus().equals(false)){
//            wmChannel.setStatus(false);
//        }

        //判断编辑后的频道名称 数据库是否已经存在
        //存在提示  DATA_EXIST(1000,"数据已经存在"),
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        query.eq(WmChannel::getName,wmChannel.getName());
        int count = this.count(query);
        if (count > 0){
            //如果已经存在  返回提示信息
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //不存在 判断当前频道是否有文章数据
        // 有则提示  频道被引用  需要article远程调用


        // 没有则保存修改后的数据
        this.updateById(wmChannel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //关键数据校验
        if (id == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //判断频道是否被禁用
        WmChannel wmChannel = this.getById(id);
        if (wmChannel.getStatus().equals(0)){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        this.removeById(id);
        return ResponseResult.okResult();
    }
}
